// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/functions/sparksql/tests/SparkFunctionBaseTest.h>

using namespace kumo::pollux::test;

namespace kumo::pollux::functions::sparksql::test {
namespace {

class ArrayAppendTest : public SparkFunctionBaseTest {
 protected:
  void testExpression(
      const std::string& expression,
      const std::vector<VectorPtr>& input,
      const VectorPtr& expected) {
    auto result = evaluate(expression, make_row_vector(input));
    assertEqualVectors(expected, result);
  }
};

TEST_F(ArrayAppendTest, intArrays) {
  const auto array_vector = make_array_vector<int64_t>(
      {{1, 2, 3, 4}, {3, 4, 5}, {7, 8, 9}, {10, 20, 30}});
  const auto elementVector = make_flat_vector<int64_t>({11, 22, 33, 44});

  VectorPtr expected = make_array_vector<int64_t>({
      {1, 2, 3, 4, 11},
      {3, 4, 5, 22},
      {7, 8, 9, 33},
      {10, 20, 30, 44},
  });
  testExpression(
      "array_append(c0, c1)", {array_vector, elementVector}, expected);
}

TEST_F(ArrayAppendTest, nullArrays) {
  const auto array_vector = make_array_vector_from_json<int64_t>(
      {"[1, 2, 3, null]", "[3, 4, 5]", "null", "[10, 20, null]"});

  const auto elementVector = make_nullable_flat_vector<int64_t>(
      {11, std::nullopt, std::nullopt, std::nullopt});

  VectorPtr expected = make_array_vector_from_json<int64_t>(
      {"[1, 2, 3, null, 11]",
       "[3, 4, 5, null]",
       "null",
       "[10, 20, null, null]"});
  testExpression(
      "array_append(c0, c1)", {array_vector, elementVector}, expected);
}

} // namespace
} // namespace kumo::pollux::functions::sparksql::test
